package com.yjd.bi.etl.promotion.service;

import java.util.Calendar;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.yjd.bi.etl.comm.BaseService;
import com.yjd.bi.etl.comm.Constants;
import com.yjd.bi.etl.sql.PromotionSqlConstants;
import com.yjd.bi.etl.sql.UserSqlConstants;
import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.util.BiUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;

@Component("promotionStatisticsService")
public class PromotionStatisticsService extends BaseService{
	private static final int  DAYOFSECOND = 86400;

	public void statisticEveryDayData() throws Exception {

		BiDatabaseInfoModel model = this.getBiDatabaseInfoModel();
		JdbcTemplate jt = BiUtil.getJdbcTemplate(model);

		// 统计历史数据
		long maxDate = jt.queryForInt(PromotionSqlConstants.GET_MAX_STATISTIC_DATE);
		
		if (maxDate == 0) {
			// 没有统计过，取网站上线运营的第一天 平台上线时间（2013年12月21日00：00：00）
			maxDate = Constants.INITIALIZATION_TIME;
		}else{
			maxDate = DateUtil.dateStringToTimestamp(maxDate+"",
					DateUtil.PATTERN_yyyyMMdd)+DAYOFSECOND;
		}
		
		//获取当天的0点的时间戳
		Calendar todayStart = Calendar.getInstance();  
        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
        todayStart.set(Calendar.MINUTE, 0);  
        todayStart.set(Calendar.SECOND, 0);  
        todayStart.set(Calendar.MILLISECOND, 0);
        long todayTime = todayStart.getTime().getTime()/1000L; 
		
		while (maxDate  < todayTime ) {
			// 统计前一天的用户数据
			statisticLastData(maxDate, jt);
			maxDate += DAYOFSECOND;// 加一天和当前时间比较，如果小于当前时间则继续统计
			
		}
	}

	/**
	 * 统计某天前一天的用户数据
	 */
	private void statisticLastData(long maxDate, JdbcTemplate jt)
			throws Exception {

		long beginDate = maxDate;
		long endDate = beginDate + DAYOFSECOND - 1;

		// 推广人数
		int spreaderCount = jt.queryForObject(PromotionSqlConstants.SPREADER_COUNT,new Object[] { beginDate, endDate},Integer.class);

		// 被推广人数
		int spreaderUserCount = jt.queryForObject(PromotionSqlConstants.SPREADER_USER_COUNT, new Object[] {beginDate, endDate},Integer.class);

		// 推广充值用户数
		int rechargeCount = jt.queryForObject(PromotionSqlConstants.SPREADER_RECHARGE_USER_COUNT,new Object[] { beginDate, endDate,beginDate, endDate},Integer.class);

		// 推广投资用户数
		int investCount = jt.queryForObject(PromotionSqlConstants.SPREADER_INVEST_USER_COUNT,new Object[] {beginDate, endDate, beginDate, endDate, beginDate, endDate},Integer.class);
		
		//推广投资金额
		Double investAmount= jt.queryForObject(PromotionSqlConstants.SPREADER_INVEST_AMOUNT,new Object[] { endDate, beginDate, beginDate, endDate, beginDate, endDate},Double.class);

		Long staticsDate = Long.parseLong(DateUtil.timestampToDateString(
				beginDate, DateUtil.PATTERN_yyyyMMdd));
		
		logger.info("beginDate:"+beginDate+",spreaderCount:"+spreaderCount+",spreaderUserCount:"+spreaderUserCount+",rechargeCount:"+rechargeCount+",investCount:"+investCount+",investAmount:"+investAmount);
		
		try{
			jt.update(PromotionSqlConstants.SPREADERSTATISTIC_INSERTSQL,staticsDate,spreaderCount,spreaderUserCount,rechargeCount,investCount,investAmount,FrameUtil.getTime());
		}catch(Exception e){
			logger.info("批量处理插入失败，处理sql:"+PromotionSqlConstants.SPREADERSTATISTIC_INSERTSQL+"处理数据:"+staticsDate+"错误信息:"+e);
		}

	}
}
